#!/bin/bash

usage(){
        echo "USAGE : ./srv start/stop "
}

if [ $# -ne 1 ];
then
    usage
    exit
fi

if [ $1 == "start" ]; then
	insmod /usr/local/httcsec/tss/kernel/read_sysram.ko
	insmod /usr/local/httcsec/tss/kernel/httctdd.ko httcsec_messsage_prot=24
	insmod /usr/local/httcsec/tss/kernel/tddl.ko sync=1
	if ps -ef | grep -v grep | grep "tpcmproxy" > /dev/null
	then
		echo "tpcmproxy process is already running."
	else
		/usr/local/httcsec/tss/tpcmproxy & 
		sleep 8
	fi
	source /usr/local/httcsec/tss/symbol.sh
	insmod /usr/local/httcsec/tss/kernel/httctcs.ko k_kallsyms_lookup_name=$kallsyms_lookup_name k_do_invalidatepage=$do_invalidatepage

	insmod /usr/local/httcsec/tss/kernel/httctcs-ex.ko
	
	chmod 666 /dev/httctcs
	chmod 666 /dev/tcm_ttd
	chmod 666 /dev/tpcm_ttd

elif [ $1 == "stop" ]; then
	rmmod httctcs_ex
	rmmod httctcs
	#killall tpcmproxy
	kill -9 `pidof tpcmproxy` >/dev/null 2>&1
	rmmod tddl
	while true
        do
                refcount=`lsmod | egrep "^httctdd" | awk '{print $3}'`
                if [ -z "$refcount" ]; then
                        break
                fi
                if [ "$refcount" -ge "1" ]; then
                        echo "httctdd in use, waiting..."
                        sleep 1
                else
                        break
                fi
        done
	rmmod httctdd
	rmmod read_sysram
else
	usage
	exit
fi
